package com.ibangnuo.framework.newLandpay.impl.request;

import com.alibaba.fastjson.JSONObject;
import com.ibangnuo.framework.newLandpay.api.NewLandpayRequest;
import com.ibangnuo.framework.newLandpay.impl.common.ResponseClient;
import com.ibangnuo.framework.newLandpay.impl.common.api.ResponseClientApi;
import com.ibangnuo.framework.newLandpay.impl.response.NewLandMCCSearch;
import com.ibangnuo.framework.newLandpay.impl.response.NewLandMCCSearchResponse;
import com.ibangnuo.framework.newLandpay.util.HttpParamsUtils;
import com.ibangnuo.framework.newLandpay.util.MD5;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020-01-10 14:19
 */
@Data
@NoArgsConstructor
public class NewLandMCCSearchRequest implements NewLandpayRequest {
    private static Logger LOGGER = LoggerFactory.getLogger(NewLandMCCSearchRequest.class);
    private Map<String,String> bizContent;//不进行加密的参数
    private Map<String,String> pre;//进行加密的参数
    private final String URL = "http://sandbox.starpos.com.cn/emercapp";
    @Override
    public NewLandMCCSearchResponse execute(String key) {
        pre.put("serviceId", "6060203");//交易码
        String preStr = HttpParamsUtils.buildPayValues(pre, false, true);
        String sign = MD5.sign(preStr, key, "UTF-8");
        pre.put("signValue",sign);
        bizContent.putAll(pre);
        ResponseClientApi responseClientApi = new ResponseClient(URL);
        String bizContentJson = JSONObject.toJSONString(bizContent);
        JSONObject jsonObject = JSONObject.parseObject(bizContentJson);
        String response = responseClientApi.request(jsonObject);
        LOGGER.info("新大陆接口响应结果{}",response);
        Map<String,Object> map = JSONObject.parseObject(response, Map.class);
        if(map !=null){
            NewLandMCCSearchResponse newLandMCCSearchResponse = new NewLandMCCSearchResponse();
            newLandMCCSearchResponse.setMsgCd(String.valueOf(map.get("msg_cd")));
            newLandMCCSearchResponse.setMsgDat(String.valueOf(map.get("msg_dat")));
            newLandMCCSearchResponse.setSignValue(String.valueOf(map.get("signValue")));
            newLandMCCSearchResponse.setBody(JSONObject.toJSONString(map));
            List<Map> list = (List) map.get("REC");
            List<NewLandMCCSearch> newLandMCCSearchList = new ArrayList<>();
            for (Map map1 : list) {
                NewLandMCCSearch newLandMCCSearch = new NewLandMCCSearch();
                newLandMCCSearch.setMccCd(String.valueOf(map1.get("mcc_cd")));
                newLandMCCSearch.setMccNm(String.valueOf(map1.get("mcc_nm")));
                newLandMCCSearch.setSupMccCd(String.valueOf(map1.get("sup_mcc_cd")));
                newLandMCCSearch.setSupMccNm(String.valueOf(map1.get("sup_mcc_nm")));
                newLandMCCSearch.setMccTyp(String.valueOf(map1.get("mcc_typ")));
                newLandMCCSearch.setMccTypNm(String.valueOf(map1.get("mcc_typ_nm")));
                newLandMCCSearchList.add(newLandMCCSearch);
            }
            newLandMCCSearchResponse.setRec(newLandMCCSearchList);
            return newLandMCCSearchResponse;
        }
        return null;
    }
}
